<template>
  <div>
                                                                    <!-- 列表排序和搜索排序 -->
    <el-table
      :data="tableData.filter(data => !search || data.uname.toLowerCase().includes(search.toLowerCase()))"
      style="width: 100%"
      height="504px"
      :stripe="true"
      :highlight-current-row="true"
    >
      <el-table-column type="index" :index="indexMethod"></el-table-column>      <!-- 索引 -->
      <el-table-column type="expand">                                            <!-- 详情展示 -->
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="用户密码">
              <span>{{ props.row.upwd}}</span>
            </el-form-item>
            <el-form-item label="性别">
              <span>{{ props.row.usex}}</span>
            </el-form-item>
            <el-form-item label="特权">
              <span>{{ props.row.vip}}</span>
            </el-form-item>
            <el-form-item label="用户头像">
              <img :src="props.row.uheader" alt="用户头像" class="pimg" />
            </el-form-item>
          </el-form>
        </template>        
      </el-table-column>
      <el-table-column label="用户 ID" prop="uid"></el-table-column>                 <!-- 列表展示 -->
      <el-table-column label="用户名称" prop="uname"></el-table-column>
      <el-table-column align="right">                                                <!-- 搜索 -->    
        <template slot="header" slot-scope="scope">   
          <el-button type="danger" round class="add" @click="handAdd(scope.row)">+ 用户</el-button>       
          <el-input v-model="search" size="mini" placeholder="请输入用户名搜索" class="search"/>
        </template>
        <template slot-scope="scope">                                                <!-- 操作框操作 -->
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row) ">操作</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, tableData)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination                                      
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage1"
      :page-size="10"
      :background="true"
      layout="total,prev, pager, next, jumper"
      :total="this.arr.length||1"
    >                                                                                 <!-- 分页 -->
    </el-pagination>
    <el-dialog title="用户修改" :visible.sync="dialogFormVisible">                     <!-- 修改框 -->
      <el-form :model="form">
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="form.uname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="用户密码" :label-width="formLabelWidth">
          <el-input v-model="form.upwd" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="用户特权" :label-width="formLabelWidth">
          <el-input v-model.number="form.vip" autocomplete="off"></el-input>
        </el-form-item>
        
        <el-form-item label="用户头像" :label-width="formLabelWidth">
          <el-input v-model="form.uheader" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select v-model="form1.usex" placeholder="请选择商品属性">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
                                                                                      <!-- 修改框底部操作 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false;handsure()">确 定</el-button>
      </div>
    </el-dialog>


<el-dialog title="添加用户" :visible.sync="dialogFormVisible1">                     <!-- 新增用户 -->
      <el-form :model="form1">
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="form1.uname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="用户密码" :label-width="formLabelWidth">
          <el-input v-model="form1.upwd" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
                                                                                      <!-- 增加用户底部操作 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible1 = false;handsure1()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>


<script>
import * as api from "@/api/getProlist";
export default {
  name: "proList",
  data() {
    return {
      list: [],
      currentPage1: 1,                      //初始页
      search: "",
      arr: [],
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible1:false,
      id: "",
      page: 1,                              //当前页
      ag: "",                               //当前条
      form: {
        uheader:"",
        uname: "",
        upwd:"",
        vip:"",
      },
      form1: {
        uname: "",
        upwd:"",
      },
      formLabelWidth: "120px"
    };
  },
  methods: {
    indexMethod(index) {
      return index+(this.page-1)*10+1;
      },
    handleCurrentChange(val) {            //分页改变
      this.page = `${val}`;
      this.getarr(this.page - 1);
    },
    handleEdit(index, row) {              //修改用户
      this.dialogFormVisible = true;
      Object.assign(this.form, {
        uheader: row.uheader,
        uname: row.uname,
        upwd: row.upwd,
        usex:row.usex,
        vip: row.vip,
        uid:row.uid,
      });
      this.id = row.uid;
      this.ag = index;
    },
    handsure() {                                                          //修改确认
      api.updateUsers(this.form).then(data => {
        console.log(data);
        Object.assign(this.arr[this.ag + (this.page - 1) * 10], this.form); //页面上新数据替代老数据并展示新的数据
      });
    },
    handAdd(){
      Object.assign(this.form1, {
        uname: "",
        upwd: "",
      });
      this.dialogFormVisible1 = true;                                      //增加用户
    },
    handsure1(){
      Object.assign(this.form1, {
        uname: document.getElementsByClassName("el-input__inner")[2].value,
        upwd: document.getElementsByClassName("el-input__inner")[3].value,
      });
      api.addUser(this.form1).then(data => {
        console.log(data)
        if(data.data.code==200){
          this.arr.unshift(data.data.data)
          this.getarr(0);
          alert(data.data.msg);
        }else{
          alert("添加失败")
        }
      });
    },
    handleDelete(index, row) {                                           //删除商品
      if(confirm("确认删除本用户?")){
      api.delUser({uid:row[index].uid}).then(data => {});
      this.arr.splice(index + (this.page - 1) * 10, 1);
      row.splice(index, 1);
      }
    },
    getarr(index) {                                                     //分页函数封装
      this.tableData = [];
      index = index < 0 ? 0 : index > this.arr.length ? this.arr.length : index;
      for (var i = index * 10; i < (index + 1) * 10; i++) {
        this.tableData.push(this.arr[i]);
      }
    }
  },
  mounted() {
    api.getUser({}).then(data => {
                                      //获取用户列表
      data.data.result.map((item, i) => {
        this.arr.push(item);
      });
      this.getarr(0);                   //默认第一页
    });
  }
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-main {
  overflow: visible;
}
.el-pagination {
  margin: 20px 0;
}
.pimg {
  width: 150px;
  height: 130px;
}
.add{
  float: left;
  width: 80px;
  height: 28px;
  text-indent: 4px;
  line-height: 3px;
  margin-top: 5px;
  font-size: 13px;
  margin-left: -25px
}
.search{
  float: right;
  width: 190px
}
</style>